package cn.samler.account_manager.service.impl;

import cn.samler.account_manager.entity.Account;
import cn.samler.account_manager.mapper.AccountMapper;
import cn.samler.account_manager.model.Page;
import cn.samler.account_manager.model.PageResponse;
import cn.samler.account_manager.model.Response;
import cn.samler.account_manager.model.SearchType;
import cn.samler.account_manager.model.req.AccountAddRequest;
import cn.samler.account_manager.model.req.AccountUpdateRequest;
import cn.samler.account_manager.service.AccountService;
import cn.samler.account_manager.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AccountServiceImpl implements AccountService {
    private final AccountMapper accountMapper;

    public AccountServiceImpl(AccountMapper accountMapper) {
        this.accountMapper = accountMapper;
    }

    @Override
    public Response add(AccountAddRequest request) {
        Account account = accountMapper.findByUsername(request.getUsername());
        if (account != null) {
            return Response.builder().ret(Response.Ret.FAILURE).msg("已经存在相同用户名的账号").build();
        }

        account = new Account();
        account.setUsername(request.getUsername());
        account.setPassword(request.getPassword());
        account.setIsIdentity(request.getIsIdentity());
        account.setExecDate(request.getExecDate());
        account.setExecTimesPerDay(request.getExecTimesPerDay());

        if (accountMapper.insert(account) > 0) {
            return Response.builder().ret(Response.Ret.SUCCESS).msg("添加成功").build();
        }

        log.error("Failed to add account: {}", account);
        return Response.builder().ret(Response.Ret.FAILURE).msg("添加失败").build();
    }

    @Override
    public Response delete(Integer id) {
        Account account = new Account();
        account.setId(id);

        if (accountMapper.delete(account) > 0) {
            return Response.builder().ret(Response.Ret.SUCCESS).msg("删除成功").build();
        }

        log.error("Failed to delete account: {}", account);
        return Response.builder().ret(Response.Ret.FAILURE).msg("删除失败").build();
    }

    @Override
    public Response update(AccountUpdateRequest request) {
        Account account = accountMapper.findById(request.getId());
        if (account == null) {
            return Response.builder().ret(Response.Ret.FAILURE).msg("该账号不存在").build();
        }

        account.setUsername(request.getUsername());
        account.setPassword(request.getPassword());
        account.setIsIdentity(request.getIsIdentity());
        account.setExecDate(request.getExecDate());
        account.setExecTimesPerDay(request.getExecTimesPerDay());
        if (accountMapper.update(account) > 0) {
            return Response.builder().ret(Response.Ret.SUCCESS).msg("修改成功").build();
        }

        log.error("Failed to update account: {}", account);
        return Response.builder().ret(Response.Ret.FAILURE).msg("修改失败").build();
    }

    @Override
    public Response list(Page page) {
        PageResponse resp = new PageResponse();

        List<Account> list = accountMapper.listByPage(PageUtil.getStartRows(page), page.getOffset());
        int count = accountMapper.countAll();

        resp.setTotal(count);
        resp.setList(list);
        return Response.builder().ret(Response.Ret.SUCCESS).msg("查询成功").data(resp).build();
    }

    @Override
    public Response search(Integer page, Integer offset, Integer type, Object keyword) {
        PageResponse resp = new PageResponse();

        // 字段查询方式
        boolean fuzzyQuery = type == SearchType.FAILED_USERNAME || type == SearchType.FAILED_PASSWORD;
        List<Account> list = accountMapper.searchByPage(SearchType.FAILED_MAP.get(type), keyword, fuzzyQuery,
                PageUtil.getStartRows(page, offset), offset);

        int count = accountMapper.countBy(SearchType.FAILED_MAP.get(type), keyword, fuzzyQuery);

        resp.setTotal(count);
        resp.setList(list);
        return Response.builder().ret(Response.Ret.SUCCESS).msg("查询成功").data(resp).build();
    }
}
